package visaoInterface;

import dao.DaoPessoa;
import dao.DaoCliente;
import dao.DaoEndereco;
import javax.swing.JOptionPane;
import modelo.dominio.*;

public class ChamaCliente extends javax.swing.JDialog {

    /** Creates new form ChamaCliente */ 
    public ChamaCliente(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLCNome = new javax.swing.JLabel();
        jLCCpf = new javax.swing.JLabel();
        jLCTelefone = new javax.swing.JLabel();
        jLCRua = new javax.swing.JLabel();
        jLCNumero = new javax.swing.JLabel();
        jTFCNome = new javax.swing.JTextField();
        jTFCRua = new javax.swing.JTextField();
        jLCBairro = new javax.swing.JLabel();
        jTFCBairro = new javax.swing.JTextField();
        jLCEndereco = new javax.swing.JLabel();
        jBCCancelar = new javax.swing.JButton();
        jBCSalvar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jBCLimpar = new javax.swing.JButton();
        jTFCNumero = new javax.swing.JTextField();
        jTFCCpf = new javax.swing.JFormattedTextField();
        jTFCTelefone = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Cliente"));

        jLCNome.setText("Nome*");

        jLCCpf.setText("CPF*");

        jLCTelefone.setText("Telefone");

        jLCRua.setText("Rua");

        jLCNumero.setText("Número");

        jTFCNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFCNomeActionPerformed(evt);
            }
        });

        jLCBairro.setText("Bairro");

        jLCEndereco.setText("Endereço:");

        jBCCancelar.setText("Cancelar");
        jBCCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCCancelarActionPerformed(evt);
            }
        });

        jBCSalvar.setText("Salvar");
        jBCSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCSalvarActionPerformed(evt);
            }
        });

        jLabel1.setText("* Campos Obrigatórios");

        jBCLimpar.setText("Limpar");
        jBCLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCLimparActionPerformed(evt);
            }
        });

        try {
            jTFCCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jTFCTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLCEndereco)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLCBairro)
                            .addComponent(jLCRua))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFCRua, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jLCNumero)
                                .addGap(18, 18, 18)
                                .addComponent(jTFCNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTFCBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                        .addComponent(jBCLimpar)
                        .addGap(18, 18, 18)
                        .addComponent(jBCSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(jBCCancelar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLCCpf)
                            .addComponent(jLCNome))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFCCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLCTelefone)
                                .addGap(18, 18, 18)
                                .addComponent(jTFCTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                            .addComponent(jTFCNome, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFCNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLCNome))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLCCpf)
                    .addComponent(jLCTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFCCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFCTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLCEndereco)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLCRua)
                    .addComponent(jTFCRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLCNumero)
                    .addComponent(jTFCNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLCBairro)
                    .addComponent(jTFCBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBCCancelar)
                        .addComponent(jBCSalvar)
                        .addComponent(jBCLimpar))
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTFCNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFCNomeActionPerformed

        // TODO add your handling code here:}//GEN-LAST:event_jTFCNomeActionPerformed
    }
        private void jBCCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCCancelarActionPerformed
            this.dispose();
    }//GEN-LAST:event_jBCCancelarActionPerformed

    private void jBCSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCSalvarActionPerformed
        //mosta na tabela os dados dos clientes.
        if(jTFCNome.getText().equals("") || jTFCCpf.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencher os campos obrigatórios.");
        }
        else{
            
        String cpf = jTFCCpf.getText();        
        if (cpf.length() > 11){
        cpf = cpf.replaceAll("\\.", "");
        cpf = cpf.replaceAll("-","");
        }
        Pessoa p = new Pessoa();
        if (p.verificaCpf(cpf) && p.validaCpf(cpf)){ 
            int codEndereco=0,codCliente=0;
            Cliente c = new Cliente();
            Endereco e = new Endereco();
            DaoCliente dc = new DaoCliente();
            DaoPessoa dp = new DaoPessoa();
            DaoEndereco de = new DaoEndereco();
            p.setNome(jTFCNome.getText());
            p.setCpf(jTFCCpf.getText());
            p.setTelefone(jTFCTelefone.getText());
            c.setCpf(jTFCCpf.getText());
            e.setRua(jTFCRua.getText());
        if (jTFCNumero.getText().equals("")){
            e.setNumero(0);
        }else{
            e.setNumero(Integer.parseInt(jTFCNumero.getText()));
        }
        e.setBairro(jTFCBairro.getText());
        de.insere(e);
        codEndereco = de.retornaCodUltimoRegistro();
        dc.insere(c);
        codCliente = dc.retornaCodUltimoRegistro();
        dp.insereCliente(p,codEndereco,codCliente);
        jTFCNome.setText("");
        jTFCCpf.setText("");
        jTFCTelefone.setText("");
        jTFCRua.setText("");
        jTFCNumero.setText("");
        jTFCBairro.setText("");
        JOptionPane.showMessageDialog(null, "O Cliente foi cadastrado com sucesso.");
        }else{
        JOptionPane.showMessageDialog(null, "CPF não corresponde a um CPF valido.");
        jTFCCpf.setText("");
        }
        }
    }//GEN-LAST:event_jBCSalvarActionPerformed

    private void jBCLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCLimparActionPerformed
        jTFCNome.setText("");
        jTFCCpf.setText("");
        jTFCTelefone.setText("");
        jTFCRua.setText("");
        jTFCNumero.setText("");
        jTFCBairro.setText("");
    }//GEN-LAST:event_jBCLimparActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChamaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChamaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChamaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChamaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

          
            public void run() {
                ChamaCliente dialog = new ChamaCliente(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

               
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCCancelar;
    private javax.swing.JButton jBCLimpar;
    private javax.swing.JButton jBCSalvar;
    private javax.swing.JLabel jLCBairro;
    private javax.swing.JLabel jLCCpf;
    private javax.swing.JLabel jLCEndereco;
    private javax.swing.JLabel jLCNome;
    private javax.swing.JLabel jLCNumero;
    private javax.swing.JLabel jLCRua;
    private javax.swing.JLabel jLCTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTFCBairro;
    private javax.swing.JFormattedTextField jTFCCpf;
    private javax.swing.JTextField jTFCNome;
    private javax.swing.JTextField jTFCNumero;
    private javax.swing.JTextField jTFCRua;
    private javax.swing.JFormattedTextField jTFCTelefone;
    // End of variables declaration//GEN-END:variables


}
